﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using Lavka.Models;
using Mvc.Mailer;

namespace Lavka.Core
{
    public class SmtpSettings
    {
        public string SmtpHost { get; set; }
        public int SmtpPort { get; set; }
        public bool UseDefaultCredentials { get; set; }
        public string SmtpUser { get; set; }
        public string SmtpPassword { get; set; }
        public bool EnableSsl { get; set; }
        public string AdminEmail { get; set; }

        public SmtpSettings()
        {
            LavkaDbEntities context = new LavkaDbEntities();
            List<Settings> settings = context.Settings.ToList();
            SmtpHost = settings.Where(s => s.Name == "SmtpHost").First().Value;
            SmtpPort = Convert.ToInt32(settings.Where(s => s.Name == "SmtpPort").First().Value);
            UseDefaultCredentials = Convert.ToBoolean(settings.Where(s => s.Name == "UseDefaultCredentials").First().Value);
            SmtpUser = settings.Where(s => s.Name == "SmtpUser").First().Value;
            SmtpPassword = settings.Where(s => s.Name == "SmtpPassword").First().Value;
            EnableSsl = Convert.ToBoolean(settings.Where(s => s.Name == "EnableSsl").First().Value);
            AdminEmail = settings.Where(s => s.Name == "AdminEmail").First().Value;
        }
    }
    public static class SettingsManager
    {
        public static SmtpSettings _smtpSettings;
        public static SmtpSettings SmtpSettings
        {
            get
            {
                if (_smtpSettings == null)
                {
                    _smtpSettings = new SmtpSettings();
                }
                return _smtpSettings;
            }
            set { _smtpSettings = value; }
        }

        public static ISmtpClient GetSmtpClient()
        {
            var smtpClient = new SmtpClient(SmtpSettings.SmtpHost, SmtpSettings.SmtpPort)
            {
                EnableSsl = SmtpSettings.EnableSsl,
                UseDefaultCredentials = SmtpSettings.UseDefaultCredentials
            };
            var credential = new NetworkCredential(SmtpSettings.SmtpUser, SmtpSettings.SmtpPassword);
            smtpClient.Credentials = credential;

            SmtpClientBase client = new SmtpClientWrapper(smtpClient);
            return client;
        }
        public static bool SaveSmtpSettings(LavkaDbEntities context, SmtpSettings settings)
        {
            try
            {
                Settings smtpHost = context.Settings.Where(s => s.Name == "SmtpHost").First();
                smtpHost.Value = settings.SmtpHost;
                Settings smtpPort = context.Settings.Where(s => s.Name == "SmtpPort").First();
                smtpPort.Value = settings.SmtpPort.ToString();
                Settings smtpDefaultCridentials = context.Settings.Where(s => s.Name == "UseDefaultCredentials").First();
                smtpDefaultCridentials.Value = settings.UseDefaultCredentials.ToString();
                Settings smtpUser = context.Settings.Where(s => s.Name == "SmtpUser").First();
                smtpUser.Value = settings.SmtpUser;
                Settings smtpPassword = context.Settings.Where(s => s.Name == "SmtpPassword").First();
                smtpPassword.Value = settings.SmtpPassword;
                Settings enableSsl = context.Settings.Where(s => s.Name == "EnableSsl").First();
                enableSsl.Value = settings.EnableSsl.ToString();
                Settings adminEmail = context.Settings.Where(s => s.Name == "AdminEmail").First();
                adminEmail.Value = settings.AdminEmail.ToString();
                context.SaveChanges();
                _smtpSettings = new SmtpSettings();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}